<?php
class ControllerExtensionModuleFeatured extends Controller {
    /**
     * @param $setting
     * @param $module_id - 此参数是后加的，之前返回 setting 中没有对应的 module_id
     * @see \ControllerCommonContentTop::index
     * @see dw-oc/catalog/controller/common/content_top.php:60
     *
     * @return string|void
     * @throws Exception
     */
	public function index($setting, $module_id, $index) {
	    if (empty($setting['product'])) {
			return;
		}

		$this->load->language('extension/module/featured');
		$this->load->model('catalog/product');

		if (!$setting['limit']) {
			$setting['limit'] = 4;
		}

		$data['products'] = array();
		foreach ($setting['product'] as $product_id) {
			$result = $this->model_catalog_product->getProduct($product_id);
			if (!$result) {
				continue;
			}

			$data['products'][] = $this->model_catalog_product->handleSingleProduct($result, $setting['width'], $setting['height']);

			if (count($data['products']) >= (int)$setting['limit']) {
				break;
			}
		}

        $search_query = http_build_query([
            'search_type' => 'featured',
            'module_id' => $module_id
        ]);
        $data['action_more'] = $this->url->link('product/search', $search_query);

        if ($data['products']) {
            $data['module_index'] = $index;
			return $this->load->view('extension/module/featured', $data);
		}
	}
}
